package com.easychat.controller;

import com.easychat.common.LeaseException;
import com.easychat.common.Result;
import com.easychat.dto.TokenUserInfoDto;
import com.easychat.dto.UserContactSearchResultDto;
import com.easychat.entity.*;
import com.easychat.entity.query.UserContactApplyQuery;
import com.easychat.entity.query.UserContactQuery;
import com.easychat.enums.ExceptionCodeEnum;
import com.easychat.enums.UserContactTypeEnum;
import com.easychat.service.UserContactApplyService;
import com.easychat.service.UserContactService;
import com.easychat.service.UserInfoService;
import com.easychat.utils.LoginUserHolder;
import com.easychat.vo.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * (UserContact)表控制层
 *
 * @author makejava
 * @since 2024-10-08 20:08:18
 */
@RestController
@RequestMapping("/userContact")
public class UserContactController {

    @Resource
    private UserContactService userContactService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserContactApplyService userContactApplyService;


    //搜索
    @GetMapping("/search")
    public Result<UserContactSearchResultDto> search(@RequestParam @NotEmpty String contactId){
//        TokenUserInfoDto tokenUserInfoDto = new TokenUserInfoDto();
//        tokenUserInfoDto.setToken("12");
//        tokenUserInfoDto.setUserId("root");
//        tokenUserInfoDto.setAdmin(true);
//        tokenUserInfoDto.setNickName("化尽纤尘");
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        UserContactSearchResultDto searchResultDto = userContactService.searchContact(tokenUserInfoDto.getUserId(),contactId);
        return Result.ok(searchResultDto);
    }
    //添加好友

    @PostMapping("/applyAdd")
    public Result applyAdd(@NotEmpty String contactId,String applyInfo){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        Integer joinType = userContactService.applyAdd(tokenUserInfoDto,contactId,applyInfo);
        return Result.ok();

    }
    //展示申请列表
    @GetMapping("/loadApply")
    public Result<UserContactApply> loadApply(Integer pageNo,Integer pageSize){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        UserContactApplyQuery userContactApplyQuery = new UserContactApplyQuery();
        userContactApplyQuery.setReceiveUserId(tokenUserInfoDto.getUserId());
        userContactApplyQuery.setQueryContactInfo(true);
        Result<UserContactApply> userContactApplyList = userContactApplyService.selectByReceiveIdPage(pageRequest,userContactApplyQuery);
        return userContactApplyList;
    }

    //处理申请
    @PostMapping("/dealWithApply")
    public Result dealWithApply(@NotEmpty Integer applyId,@NotEmpty Integer status){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        userContactApplyService.dealWithApply(tokenUserInfoDto.getUserId(),applyId,status);
        return Result.ok();
    }

    @GetMapping("/loadContact")
    public Result<List<UserContact>> loadContact(@NotEmpty String contactType){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        UserContactTypeEnum contactTypeEnum = UserContactTypeEnum.getByName(contactType);
        if(null == contactTypeEnum){
            throw new LeaseException(ExceptionCodeEnum.ERROR_REQUEST);
        }
        UserContactQuery userContactQuery = new UserContactQuery();
        userContactQuery.setUserId(tokenUserInfoDto.getUserId());
        userContactQuery.setContactType(contactTypeEnum.getType());
        if(UserContactTypeEnum.USER == contactTypeEnum){
            userContactQuery.setQueryContactUserInfo(true);
        } else if (UserContactTypeEnum.GROUP == contactTypeEnum) {
            userContactQuery.setQueryGroupInfo(true);
            userContactQuery.setExcludeMyGroup(true);
        }
        userContactQuery.setStatusArray(new Integer[]{
                1 , 3 ,5 //不能查询的几种状态
        });

        List<UserContact> userContactList = userContactService.selectUserOrGroupList(userContactQuery);
        return Result.ok(userContactList);
    }

    /**
     * 获取联系人消息 不一定是好友
     * @param contactId
     * @return
     */
    @GetMapping("/getContactInfo")
    public Result<UserInfoVO> getContactInfo(@NotEmpty String contactId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();

        UserInfo userInfo = userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userInfoVO);
        userInfoVO.setContactStatus(0);

        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(tokenUserInfoDto.getUserId(), contactId);
        if(userContact != null){
            userInfoVO.setContactStatus(1);
        }


        return Result.ok(userInfoVO);
    }

    /**
     * 获取联系人消息 一定是好友
     * @param contactId
     * @return
     */
    @GetMapping("/getContactUserInfo")
    public Result<UserInfoVO> getContactUserInfo(@NotEmpty String contactId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(tokenUserInfoDto.getUserId(), contactId);
        if(userContact == null || (userContact.getStatus() != 1 && userContact.getStatus()!=3 && userContact.getStatus()!= 5)){
                throw new LeaseException(ExceptionCodeEnum.ERROR_REQUEST);
        }

        UserInfo userInfo = userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userInfoVO);
        userInfoVO.setContactStatus(userContact.getStatus());

        return Result.ok(userInfoVO);
    }

    @PutMapping("/delContact")
    public Result delContact(@NotEmpty String contactId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        userContactService.removeUserContact(tokenUserInfoDto.getUserId(), contactId,2);
        return Result.ok();
    }
    @PutMapping("/addContact2BlackList")
    public Result addContact2BlackList(@NotEmpty String contactId){
        TokenUserInfoDto tokenUserInfoDto = LoginUserHolder.get();
        userContactService.removeUserContact(tokenUserInfoDto.getUserId(), contactId,3);
        return Result.ok();
    }






    /**
     * 分页查询
     *
     * @param userContact 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<UserContact>> queryByPage(UserContact userContact, PageRequest pageRequest) {
        return ResponseEntity.ok(this.userContactService.queryByPage(userContact, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<UserContact> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.userContactService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param userContact 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserContact> add(UserContact userContact) {
        return ResponseEntity.ok(this.userContactService.insert(userContact));
    }

    /**
     * 编辑数据
     *
     * @param userContact 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<UserContact> edit(UserContact userContact) {
        return ResponseEntity.ok(this.userContactService.update(userContact));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.userContactService.deleteById(id));
    }

}

